package com.kch.system.service.impl;

import com.kch.system.domain.SysBusiness;
import com.kch.system.domain.SysUser;
import com.kch.system.domain.UserBrowse;
import com.kch.system.mapper.UserBrowseMapper;
import com.kch.system.req.UserCollectBrowseReq;
import com.kch.system.service.IUserBrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserBrowseServiceImpl implements IUserBrowseService {

    @Autowired
    private UserBrowseMapper userBrowseMapper;

    @Override
    @Transactional
    public int insertUserBrowse(UserCollectBrowseReq req, SysUser sysUser) {
        //先查询是是否超过20条,如果超过,删除多余的
        userBrowseMapper.deleteOverTwenty(sysUser.getUserId().intValue());

        UserBrowse userBrowse = new UserBrowse();
        userBrowse.setBusId(req.getBusId());
        userBrowse.setCloumnCode(req.getCloumnCode());
        userBrowse.setUserId(sysUser.getUserId().intValue());
        userBrowse.setCreateTime(new Date());
        userBrowse.setCreateBy(sysUser.getLoginName());
        return userBrowseMapper.insert(userBrowse);
    }

    @Override
    public List<SysBusiness> selectBrowsePage(SysUser sysUser) {
        return userBrowseMapper.selectBrowseByUserId(sysUser.getUserId().intValue());
    }

    public static void main(String[] args) {
        for(int i = 2; i < 25; i++){
            System.out.println("INSERT INTO `user_browse` (`user_id`, `bus_id`, `cloumn_code`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('" +  i + "', '" + i + "', '" + i +"', '', NULL, '', NULL);");
        }
    }
}
